import { Component, OnInit } from "@angular/core";
import { FormGroup, FormControl, FormArray } from "@angular/forms";
import { OutputDesignClassItem } from "./outputdesignclassitem";
import { InitDesignClassItem } from "./initdesignclassitem";
import { OutputDesignClassService } from "./outputdesignclass.service";

@Component({
  selector: "app-outputdesignclass",
  templateUrl: "./outputdesignclass.component.html",
  styleUrls: ["./outputdesignclass.component.css"],
})
export class OutputdesignclassComponent implements OnInit {
  outputDesignClassItem$: OutputDesignClassItem;
  screenId: string;
  classId: string;
  importedfiles$: OutputDesignClassItem;

  constructor(private service: OutputDesignClassService) {}

  public validateForm: FormGroup = new FormGroup({
    fieldDefinitionTempFormControl: new FormControl(),
    fieldSummaryTempFormControl: new FormControl(),
  });

  public outputdesignclassInfo = {
    processID: "1",
    classType: "1",
    classLogicNm: "1",
    fieldPackageNmTemp: "",
    fieldClassPhysicsNmTemp: "",
    fieldAnnotationTemp: "",
    fieldImplClassNmTemp: "",
    fieldClassInfoTemp: "",
    fieldTopRemarksTemp: "",
    fieldInterfaceTemp: "",

    fieldDefinitionTemp: "",
    fieldSummaryTemp: "",
    fieldmiddleRemarksTemp: "",
    fieldList: [],
    methodDefinitionTemp: "",
    methodSummaryTemp: "",
    footRemarksTemp: "",
    methodList: [],
  };

  public processIDList: any[] = [
    {
      key: "1",
      value: "KKPMC0301",
    },
    {
      key: "2",
      value: "KKPMC0302",
    },
    {
      key: "3",
      value: "KKPMC0303",
    },
  ];

  public classTypeList: any[] = [
    {
      key: "1",
      value: "DTO",
    },
    {
      key: "2",
      value: "DAO",
    },
    {
      key: "3",
      value: "AJ",
    },
  ];
  public classLogicNmList: any[] = [
    {
      key: "1",
      value: "代理店参照権限テーブルチェックリストDTO",
    },
    {
      key: "2",
      value: "代理店参照権限テーブルチェックレスポンスDTO",
    },
    {
      key: "3",
      value: "代理店参照権限テーブルチェック返却情報DTO",
    },
  ];

  ngOnInit(): void {}

  doSearch() {
    sessionStorage.setItem("screenId", this.screenId);
    sessionStorage.setItem("designTypeId", this.classId);
    if (this.screenId != null) {
      this.service
        .searchByClassID(this.screenId, this.classId)
        .subscribe((data) => {
          this.importedfiles$ = data;

          if (this.importedfiles$ === null) {
            return;
          } else {
            this.outputdesignclassInfo.fieldPackageNmTemp = this.importedfiles$.fieldImplClassNm;
            this.outputdesignclassInfo.fieldClassPhysicsNmTemp = this.importedfiles$.fieldClassPhysicsNm;
            //this.outputdesignclassInfo.fieldAnnotationTemp = this.importedfiles$.fieldAnnotation;
            //this.outputdesignclassInfo.fieldImplClassNmTemp = this.importedfiles$.fieldImplClassNm;
            //this.outputdesignclassInfo.fieldInterfaceTemp = this.importedfiles$.fieldInterface;
            this.outputdesignclassInfo.fieldClassInfoTemp = this.importedfiles$.fieldClassInfo;
            this.outputdesignclassInfo.fieldTopRemarksTemp = this.importedfiles$.fieldInterface;
          }
        });
    }
  }

  onRegister(): void {
    const outputDesignClassItem: OutputDesignClassItem = {
      fieldDefinition: this.outputdesignclassInfo.fieldDefinitionTemp,
      fieldSummary: this.outputdesignclassInfo.fieldSummaryTemp,
      fieldmiddleRemarks: this.outputdesignclassInfo.fieldmiddleRemarksTemp,
      fieldList: this.outputdesignclassInfo.fieldList,
      methodDefinition: this.outputdesignclassInfo.methodDefinitionTemp,
      methodSummary: this.outputdesignclassInfo.methodSummaryTemp,
      footRemarks: this.outputdesignclassInfo.footRemarksTemp,
      methodList: this.outputdesignclassInfo.methodList,
      fieldPackageNm: this.outputdesignclassInfo.fieldPackageNmTemp,
      fieldClassPhysicsNm: this.outputdesignclassInfo.fieldClassPhysicsNmTemp,
      fieldAnnotation: this.outputdesignclassInfo.fieldAnnotationTemp,
      fieldImplClassNm: this.outputdesignclassInfo.fieldImplClassNmTemp,
      fieldInterface: this.outputdesignclassInfo.fieldInterfaceTemp,
      fieldClassInfo: this.outputdesignclassInfo.fieldClassInfoTemp,
      fieldTopRemarks: this.outputdesignclassInfo.fieldTopRemarksTemp,
      screenId: this.screenId,
      classId: this.classId,
    };
    this.service.register(outputDesignClassItem).subscribe((data) => {
      const link = document.createElement("a");
      const blob = new Blob([data.body], { type: "application/vnd.ms-excel" });
      link.setAttribute("href", window.URL.createObjectURL(blob));
      //link.setAttribute('download', new Date().getTime() + '.xls');
      link.setAttribute(
        "download",
        "KKPM_クラス仕様書（" +
          this.outputdesignclassInfo.fieldClassPhysicsNmTemp +
          "）.xls"
      );
      link.style.visibility = "hidden";
      document.body.appendChild(link);
      link.click();
      document.body.removeChild(link);
    });
  }

  addfield() {
    const xxx = {
      fieldDefinition: this.outputdesignclassInfo.fieldDefinitionTemp,
      fieldSummary: this.outputdesignclassInfo.fieldSummaryTemp,
      fieldmiddleRemarks: this.outputdesignclassInfo.fieldmiddleRemarksTemp,
    };

    this.outputdesignclassInfo.fieldList.push(xxx);

    this.outputdesignclassInfo.fieldDefinitionTemp = "";
    this.outputdesignclassInfo.fieldSummaryTemp = "";
    this.outputdesignclassInfo.fieldmiddleRemarksTemp = "";
  }

  addMethod() {
    const xxx = {
      methodDefinition: this.outputdesignclassInfo.methodDefinitionTemp,
      methodSummary: this.outputdesignclassInfo.methodSummaryTemp,
      footRemarks: this.outputdesignclassInfo.footRemarksTemp,
    };

    this.outputdesignclassInfo.methodList.push(xxx);

    this.outputdesignclassInfo.methodDefinitionTemp = "";
    this.outputdesignclassInfo.methodSummaryTemp = "";
    this.outputdesignclassInfo.footRemarksTemp = "";
  }

  delBtn(i: number) {
    this.outputdesignclassInfo.fieldList.splice(i, 1);
  }
  delMethodBtn(i: number) {
    this.outputdesignclassInfo.methodList.splice(i, 1);
  }
}
